/*
 * @Description: 添加方案弹窗
 * @Author: Rfan
 * @Date: 2022-10-09 19:23:26
 * @LastEditTime: 2022-10-21 18:12:30
 */

import { useEffect, useRef } from 'react';
import { Button, Steps } from 'antd';
import JPModal from '@/components/JPComponents/JPModal';
import { isNullObj } from '@/utils';
import {
  AddPlanDialogContextProvider,
  EnumDialogType,
  useAddPlanDialogContext
} from './contexts/AddPlanDialogContext';
import BaseInfo from '@/view/system/ArchivesDetection/components/dialogs/AddPlan/components/BaseInfo';
import FieldCheck from '@/view/system/ArchivesDetection/components/dialogs/AddPlan/components/FieldCheck';
import CheckRules from '@/view/system/ArchivesDetection/components/dialogs/AddPlan/components/CheckRules';
import { CheckedObjType } from '@/view/system/ArchivesDetection/dictionaries';
import type { ICommonDialog } from '@/interfaces/global';
import type { ICheckedObjType } from '@/view/system/ArchivesDetection/interface';

const { Step } = Steps;

const steps = [
  {
    title: '方案基本信息',
    key: 1
  },
  {
    title: '设置检测字段',
    key: 2
  },
  {
    title: '检测规则',
    key: 3
  }
];

interface AddDictionaryDialogProps extends ICommonDialog {
  checkedObjType: ICheckedObjType;
  type: 'add' | 'edit';
  data?: any;
}

const AddPlanDialogComp = (props: AddDictionaryDialogProps) => {
  const { visible, checkedObjType, type, data, onClose } = props;
  const {
    setObjType,
    currentStep,
    levelFieldList,
    setLevelFieldList,
    setCurrentStep,
    setDialogVisible,
    setDialogType,
    currentPlanInfo,
    currentRuleInfo,
    setCurrentPlanInfo,
    setDetectionRules
  } = useAddPlanDialogContext();

  // 方案信息Ref
  const baseInfoRef = useRef<any>();
  // 检测字段Ref
  const checkFieldRef = useRef<any>();
  // 检测规则Ref
  const checkRulesRef = useRef<any>(null);
  // 步骤列表

  const stepList = steps.filter(
    (item) => !(checkedObjType.id !== CheckedObjType.Entry && item.key === 2)
  );

  useEffect(() => {
    setDialogVisible(visible);
    if (visible) {
      setDialogType(type);
      setObjType(checkedObjType);
      if (type === EnumDialogType.edit) {
        setCurrentPlanInfo(data);
      }
      setCurrentStep(0);
    } else {
      !isNullObj(currentPlanInfo) && setCurrentPlanInfo({});
      !isNullObj(currentRuleInfo) && setDetectionRules({});

      if (levelFieldList.length > 0) {
        setLevelFieldList([]);
      }
    }
  }, [visible]);

  const onCancel = () => {
    onClose();
  };

  const next = () => {
    setCurrentStep(currentStep + 1);
  };

  const prev = () => {
    if (currentStep === 2) {
      checkRulesRef.current.cacheData();
    }
    setCurrentStep(currentStep - 1);
  };

  const submitContent = () => {
    switch (currentStep) {
      case 0: {
        baseInfoRef.current.submit();
        break;
      }
      case 1: {
        if (checkedObjType.id === 1) {
          checkFieldRef.current.submit();
          next();
        }
      }
    }
  };

  const saveRules = () => {
    checkRulesRef.current.saveRules(onClose);
  };

  return (
    <JPModal
      destroyOnClose
      title={`${type === EnumDialogType.edit ? '编辑' : '新建'}${checkedObjType.name}方案`}
      visible={visible}
      width={900}
      maskClosable={false}
      draggable
      onCancel={onCancel}
      footer={[
        currentStep > 0 && (
          <Button key="prev" onClick={prev}>
            上一步
          </Button>
        ),
        currentStep < stepList.length - 1 && (
          <Button key="next" onClick={submitContent}>
            下一步
          </Button>
        ),
        <Button
          key="submit"
          type="primary"
          disabled={currentStep !== stepList.length - 1}
          onClick={() => {
            saveRules();
          }}
        >
          保存
        </Button>
      ]}
    >
      <Steps current={currentStep}>
        {stepList.map((item) => (
          <Step {...item} />
        ))}
      </Steps>
      {currentStep === 0 && <BaseInfo ref={baseInfoRef} />}
      {checkedObjType.id === CheckedObjType.Entry && currentStep === 1 && (
        <FieldCheck ref={checkFieldRef} />
      )}
      {checkedObjType.id !== CheckedObjType.Entry && currentStep === 1 && (
        <CheckRules type={checkedObjType.id} ref={checkRulesRef} />
      )}
      {currentStep === 2 && <CheckRules type={checkedObjType.id} ref={checkRulesRef} />}
    </JPModal>
  );
};

const AddPlanDialog = (props: AddDictionaryDialogProps) => (
  <AddPlanDialogContextProvider>
    <AddPlanDialogComp {...props} />
  </AddPlanDialogContextProvider>
);

export default AddPlanDialog;
